/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://jwsdp.dev.java.net/CDDLv1.0.html
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
 * add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your
 * own identifying information: Portions Copyright [yyyy]
 * [name of copyright owner]
 */
package com.probiz.estore.eai.common;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.core.util.GenericsUtils;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.eai.exception.EaiException;

/**
 * XML文件导入泛型基类  --  业务实体一次性载入内存
 * 
 * @see GenericPartialUnmarshaller  GenericPartialUnmarshaller是分段加载XML文件到内存的，一次只加载XML中的一个业务实体
 * 
 * @author pengzhirong
 */
public abstract class GenericUnmarshaller<T> {
    
	protected final transient Log	logger	= LogFactory.getLog(getClass());
	
	protected Class<T> entitySetClass = null;
	
	private  JAXBContext jaxbContext;
	
	protected EaiUtil eaiUtil = null;
	
	
	public GenericUnmarshaller(){
		entitySetClass = GenericsUtils.getSuperClassGenricType(getClass());
		
		try {
			jaxbContext = JAXBContext.newInstance(entitySetClass.getPackage().getName());
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 要处理的目标XML文件全路径，即要导入的xml文件的全路径，开发人员根据情况可覆写此方法
	 * 从配置文件eai.properties文件中读取
	 * @return
	 */
	protected String getTargetXmlFilePath(){
		return eaiUtil.getTargetXmlFilePathByEntitySetClass(entitySetClass, false);
	}
	
	/**
	 * 得到最新的要处理的xml文件全路径
	 * @return
	 */
	private String getLatestTargetXmlFilePath(){
		
		String targetXmlFilePath = getTargetXmlFilePath();
		
		String latestTargetXmlFilePath = eaiUtil.getLatestTargetXmlFilePath(targetXmlFilePath);
			
		return latestTargetXmlFilePath;
	}
	
	/**
	 * 获取业务实体集合
	 * @return
	 */
	public T getEntitySet() throws EaiException{
		
		try {
			String latestTargetXmlFilePath  = getLatestTargetXmlFilePath();
			if(latestTargetXmlFilePath==null)
				return null;
				
			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
			
	
			//要导入的XML文件
			File xmlDocument = new File(latestTargetXmlFilePath);
			if(!xmlDocument.exists()){
				logger.info("getEntitySet():"+I18nUtil.getInstance().getMessage("eaiJobLog.info.fileNotFound", new Object[]{xmlDocument.getAbsolutePath()}));
				return null;
			}
			
			JAXBElement<T> entitySetElement = (JAXBElement<T>) unMarshaller.unmarshal(xmlDocument);
			T entitySet = entitySetElement.getValue();
			
			return entitySet;
		} catch (JAXBException e) {
			throw new EaiException(I18nUtil.getInstance().getMessage("eaiJobLog.exception.xml.unmarshal"), e);
		}
		
	}
	
	/**
	 * 用户对业务实体集合的处理放此方法中，子类需要实现此方法
	 * 
	 * @see #getEntitySet 可以得到XML文件对应的业务实体集合
	 */
	public abstract void startUnmarshal() throws Exception;

	public void setEaiUtil(EaiUtil eaiUtil) {
		this.eaiUtil = eaiUtil;
	}
	
}
